package io.renren.modules.medical.controller;
import io.renren.common.result.Result;
import io.renren.modules.medical.service.WebSocketServer;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 管理端发送消息接口
 * @author Luo Sheng
 * @school YiBin university
 * @create 2023-03-28 20:22
 */

@RestController
@RequestMapping("/medical/webSocket")
public class WebSocketController {

    // 后台端主动推送数据接口 TODO：作为用户端的通讯接口，病人采用当前通信端口
    @GetMapping("/push/{cid}")
    public Result<Map<String,Object>> pushToWeb(@PathVariable String cid, String message) {
        // 先获取当前用户信息，判断当前用户是医生还是病人，医生就采用socket信道
        Map<String,Object> result = new HashMap<>();
        try {
            WebSocketServer.sendInfo(message,cid);
            result.put("status","success");
            return Result.success(result);
        } catch (IOException e) {
            e.printStackTrace();
            result.put("status","fail");
            result.put("errMsg",e.getMessage());
            return Result.fail(result);
        }
    }

}